/*
 * CornerBreakout.fx
 */

package org.frapwidgets.scene.shape.demo;

import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.paint.Paint;
import javafx.scene.shape.ArcTo;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;

/**
 * Displays a small "top-left" rounded corner.
 *
 * @author Jonathan Merritt
 */
public class CornerBreakout extends CustomNode {

  /** Fill for the node */
  public-init var fill: Paint;
  /** Stroke for the node */
  public-init var stroke: Paint;
  /** Arc radius of the node */
  public-init var arcRadius: Number;
  /** A node to display within the rounded corner, at a 45-degree angle */
  public-init var item: Node;
  /** Gap between item and the edge corner */
  public-init var itemGap: Number = 8;
  /** Size of the corner */
  public-init var size: Number;

  override protected function create () : Node {
    def cornerPath = Path {
      fill: fill
      stroke: stroke
      elements: [
        MoveTo { x: 0, y: arcRadius },
        LineTo { x: 0, y: size },
        LineTo { x: size, y: 0 },
        LineTo { x: arcRadius, y: 0 },
        ArcTo { x: 0, y: arcRadius, radiusX: arcRadius, radiusY: arcRadius }
      ]
    }

    def itemNode = Group {
      content: [ item ]
      def trval = 0.707 * size - itemGap;
      transforms: [
        Translate { x: trval, y: trval },
        Rotate { angle: -45 },
        Translate {
          x: bind -item.layoutBounds.width / 2
          y: bind -item.layoutBounds.height
        }
      ]
    }

    Group { content: [ cornerPath, itemNode ] }
  }
  
}
